﻿using System;
using System.Linq;
using Lbb.Data;
using Lbb.Web.Mvc;
using Lbb.URating.DL;
using Lbb.Common;

namespace Lbb.URating.Models
{
    /// <summary>
    /// 点击器数据列表
    /// </summary>
    /// <author>Chiron</author>
    public class U_ClickDataList : ExtDataList<U_Click>
    {
        #region Ctors
        /// <summary>
        /// 构建点击器数据列表
        /// </summary>
        public U_ClickDataList()
        {
            if (Request != null)
            {
                IsRating = GetReqBool("isRating");
                MinRating = GetReqDecimal("minRating");
                MaxRating = GetReqDecimal("maxRating");
                CategoryIdsStr = Request["categoryIds"];
            }
        }
        #endregion Ctors

        #region Props

        /// <summary>
        /// 是否为评分项
        /// </summary>
        public bool? IsRating
        {
            get
            {
                return RouteValues.isRating as bool?;
            }
            set
            {
                RouteValues.isRating = value;
            }
        }
        /// <summary>
        /// 最小分值
        /// </summary>
        public decimal? MinRating
        {
            get
            {
                return RouteValues.minRating as decimal?;
            }
            set
            {
                RouteValues.minRating = value;
            }
        }
        /// <summary>
        /// 最大分值
        /// </summary>
        public decimal? MaxRating
        {
            get
            {
                return RouteValues.maxRating as decimal?;
            }
            set
            {
                RouteValues.maxRating = value;
            }
        }
        /// <summary>
        /// 所属分类
        /// </summary>
        public string CategoryIdsStr
        {
            get
            {
                return RouteValues.categoryIds as string;
            }
            set
            {
                RouteValues.categoryIds = value;
                _categoryIdsChanged = true;
            }
        }
        private bool _categoryIdsChanged = false;
        /// <summary>
        /// 所属分类
        /// </summary>
        public Guid[] CategoryIds
        {
            get
            {
                if (_categoryIds == null || _categoryIdsChanged)
                {
                    _categoryIds = CategoryIdsStr.ToGuids(removeEmpty: true);
                    _categoryIdsChanged = false;
                }
                return _categoryIds;
            }
            set
            {
                CategoryIdsStr = value.Merger();
                _categoryIdsChanged = false;
            }
        }
        private Guid[] _categoryIds;
        #endregion Props

        #region Methods
        /// <summary>
        /// 填充数据
        /// </summary>
        /// <param name="data">数据源</param>
        public void FillData(IQueryable<U_Click> data)
        {
            if (data == null) throw new ArgumentNullException("data");

            switch (OrderBy)
            {
                default:
                case "ordering_up":
                    data = data.OrderBy(d => d.Ordering);
                    break;
                case "ordering_down":
                    data = data.OrderByDescending(d => d.Ordering);
                    break;
                case "title_up":
                    data = data.OrderBy(d => d.Title);
                    break;
                case "title_down":
                    data = data.OrderByDescending(d => d.Title);
                    break;
                case "isRating_up":
                    data = data.OrderBy(d => d.IsRating);
                    break;
                case "isRating_down":
                    data = data.OrderByDescending(d => d.IsRating);
                    break;
                case "minRating_up":
                    data = data.OrderBy(d => d.MinRating);
                    break;
                case "minRating_down":
                    data = data.OrderByDescending(d => d.MinRating);
                    break;
                case "maxRating_up":
                    data = data.OrderBy(d => d.MaxRating);
                    break;
                case "maxRating_down":
                    data = data.OrderByDescending(d => d.MaxRating);
                    break;
            }

            Items = data.GetPartialData(FromIndex, PageSize, out Total);
        }
        #endregion Methods
    }
}
